package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NLaboratoryMarshallStabilityBo;
import com.ruoyi.zhgd.domain.vo.NLaboratoryMarshallStabilityVo;
import com.ruoyi.zhgd.domain.NLaboratoryMarshallStability;
import com.ruoyi.zhgd.mapper.NLaboratoryMarshallStabilityMapper;
import com.ruoyi.zhgd.service.INLaboratoryMarshallStabilityService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 马歇尔试验Service业务层处理
 *
 * @author zyb
 * @date 2023-09-11
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NLaboratoryMarshallStabilityServiceImpl implements INLaboratoryMarshallStabilityService {

    private final NLaboratoryMarshallStabilityMapper baseMapper;

    /**
     * 查询马歇尔试验
     */
    @Override
    public NLaboratoryMarshallStabilityVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询马歇尔试验列表
     */
    @Override
    public TableDataInfo<NLaboratoryMarshallStabilityVo> queryPageList(NLaboratoryMarshallStabilityBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NLaboratoryMarshallStability> lqw = buildQueryWrapper(bo);
        Page<NLaboratoryMarshallStabilityVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询马歇尔试验列表
     */
    @Override
    public List<NLaboratoryMarshallStabilityVo> queryList(NLaboratoryMarshallStabilityBo bo) {
        LambdaQueryWrapper<NLaboratoryMarshallStability> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NLaboratoryMarshallStability> buildQueryWrapper(NLaboratoryMarshallStabilityBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NLaboratoryMarshallStability> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBaseCode()), NLaboratoryMarshallStability::getBaseCode, bo.getBaseCode());
        lqw.eq(StringUtils.isNotBlank(bo.getOrganizationId()), NLaboratoryMarshallStability::getOrganizationId, bo.getOrganizationId());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceId()), NLaboratoryMarshallStability::getDeviceId, bo.getDeviceId());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectId()), NLaboratoryMarshallStability::getProjectId, bo.getProjectId());
        lqw.eq(StringUtils.isNotBlank(bo.getEngineeringSite()), NLaboratoryMarshallStability::getEngineeringSite, bo.getEngineeringSite());
        lqw.eq(StringUtils.isNotBlank(bo.getSampleNo()), NLaboratoryMarshallStability::getSampleNo, bo.getSampleNo());
        lqw.like(StringUtils.isNotBlank(bo.getSampleName()), NLaboratoryMarshallStability::getSampleName, bo.getSampleName());
        lqw.eq(bo.getStartTime() != null, NLaboratoryMarshallStability::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, NLaboratoryMarshallStability::getEndTime, bo.getEndTime());
        lqw.eq(bo.getUploadTime() != null, NLaboratoryMarshallStability::getUploadTime, bo.getUploadTime());
        lqw.eq(bo.getInsertTime() != null, NLaboratoryMarshallStability::getInsertTime, bo.getInsertTime());
        lqw.eq(bo.getStability1() != null, NLaboratoryMarshallStability::getStability1, bo.getStability1());
        lqw.eq(bo.getFlow1() != null, NLaboratoryMarshallStability::getFlow1, bo.getFlow1());
        lqw.eq(bo.getStability2() != null, NLaboratoryMarshallStability::getStability2, bo.getStability2());
        lqw.eq(bo.getFlow2() != null, NLaboratoryMarshallStability::getFlow2, bo.getFlow2());
        lqw.eq(bo.getStability3() != null, NLaboratoryMarshallStability::getStability3, bo.getStability3());
        lqw.eq(bo.getFlow3() != null, NLaboratoryMarshallStability::getFlow3, bo.getFlow3());
        lqw.eq(bo.getStability4() != null, NLaboratoryMarshallStability::getStability4, bo.getStability4());
        lqw.eq(bo.getFlow4() != null, NLaboratoryMarshallStability::getFlow4, bo.getFlow4());
        lqw.eq(bo.getStability5() != null, NLaboratoryMarshallStability::getStability5, bo.getStability5());
        lqw.eq(bo.getFlow5() != null, NLaboratoryMarshallStability::getFlow5, bo.getFlow5());
        lqw.eq(bo.getStability6() != null, NLaboratoryMarshallStability::getStability6, bo.getStability6());
        lqw.eq(bo.getFlow6() != null, NLaboratoryMarshallStability::getFlow6, bo.getFlow6());
        lqw.eq(bo.getAverage() != null, NLaboratoryMarshallStability::getAverage, bo.getAverage());
        lqw.eq(StringUtils.isNotBlank(bo.getIsQualified()), NLaboratoryMarshallStability::getIsQualified, bo.getIsQualified());
        lqw.eq(bo.getMaxFlow() != null, NLaboratoryMarshallStability::getMaxFlow, bo.getMaxFlow());
        lqw.eq(bo.getMinFlow() != null, NLaboratoryMarshallStability::getMinFlow, bo.getMinFlow());
        lqw.eq(bo.getStabilityValue() != null, NLaboratoryMarshallStability::getStabilityValue, bo.getStabilityValue());
        lqw.eq(bo.getStabilityAverage() != null, NLaboratoryMarshallStability::getStabilityAverage, bo.getStabilityAverage());
        lqw.eq(StringUtils.isNotBlank(bo.getTestType()), NLaboratoryMarshallStability::getTestType, bo.getTestType());
        lqw.like(StringUtils.isNotBlank(bo.getEngineeringName()), NLaboratoryMarshallStability::getEngineeringName, bo.getEngineeringName());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptId()), NLaboratoryMarshallStability::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptPkid()), NLaboratoryMarshallStability::getDeptPkid, bo.getDeptPkid());
        return lqw;
    }

    /**
     * 新增马歇尔试验
     */
    @Override
    public Boolean insertByBo(NLaboratoryMarshallStabilityBo bo) {
        NLaboratoryMarshallStability add = BeanUtil.toBean(bo, NLaboratoryMarshallStability.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改马歇尔试验
     */
    @Override
    public Boolean updateByBo(NLaboratoryMarshallStabilityBo bo) {
        NLaboratoryMarshallStability update = BeanUtil.toBean(bo, NLaboratoryMarshallStability.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NLaboratoryMarshallStability entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除马歇尔试验
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
